/**
 * 创建瀑布流
 */
export const createWaterfall = (
  data: { width: number; height: number }[],
  options: {
    baseWidth: number;
    columnHeight: number[];
    defaultWidth?: number;
    defaultHeight?: number;
    widthKey?: string;
    heightKey?: string;
  }
) => {
  const { baseWidth, columnHeight } = options;
  const columnData = data.reduce(
    (array, i) => {
      let width = (i as any)[options.widthKey || "width"];
      let height = (i as any)[options.heightKey || "height"];

      if (width === 0 && options.defaultWidth) width = options?.defaultWidth;
      if (height === 0 && options.defaultHeight)
        height = options?.defaultHeight;

      const column = columnHeight[0] > columnHeight[1] ? 1 : 0;
      const heightRect = (baseWidth * height) / width;
      const waterfall = { ...i, widthRect: baseWidth, heightRect };
      array[column].push(waterfall);
      columnHeight[column] += heightRect;
      return array;
    },
    [[], []] as ((typeof data)[0] & {
      widthRect: number;
      heightRect: number;
    })[][]
  );
  return { columnWidth: baseWidth, columnData };
};
